package com.hxkj.service.article.impl;

import com.hxkj.client.service.TagsServiceClient;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.domain.po.Tags;
import com.hxkj.service.article.TagsService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagsServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:59
 * @Version 1.0
 */

@Service
@Slf4j
public class TagsServiceImpl implements TagsService {

    @Autowired
    private TagsServiceClient tagsServiceClient;


    @Override
    public void saveTags(Tags tags) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tags == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_VOID);
            }
            if (StringUtils.isEmpty(tags.getTagsName())) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_NAME);
            }
            if (tags.getSiteId() == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
            }
            resultMessageStruct = tagsServiceClient.saveTags(tags);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<Tags> getTagsList(Tags tags, PageTo pageParam) throws Exception {
        if (tags.getSiteId() == null) {
            throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
        }
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<Tags> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(tags);
        ResultPageStruct<Tags> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = tagsServiceClient.getTagsList(parameterDataStruct);
            resultPageStruct.setMessage(null);
            resultPageStruct.setCode(null);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return resultPageStruct;
    }

    @Override
    public void removeTags(Tags tags) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tags == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_VOID);
            }
            if (tags.getTagsIds().equals("")) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_VOID);
            }
            resultMessageStruct = tagsServiceClient.removeTags(tags);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifyTags(Tags tags) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tags == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_VOID);
            }
            if (tags.getTagsId() == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_VOID);
            }
            if (tags.getSiteId() == null) {
                throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
            }
            resultMessageStruct = tagsServiceClient.modifyTags(tags);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<Tags> getShowTagsList(Tags tags) throws Exception {
        if (tags.getSiteId() == null) {
            throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
        }
        List<Tags> tagsList = new ArrayList<>();
        try {
            tagsList = tagsServiceClient.getShowTagsList(tags);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return tagsList;
    }

    @Override
    public ResultPageStruct<Tags> getHotTagsList(Tags tags, PageTo pageParam) throws Exception {
        if (tags.getSiteId() == null) {
            throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
        }
        ParameterDataStruct<Tags> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(tags);
        ResultPageStruct<Tags> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = tagsServiceClient.getHotTagsList(parameterDataStruct);
            resultPageStruct.getPageInfo().setCondition(tags.getSort());//将标签定义的排序规则封装返回
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPageStruct;
    }
}
